const app = getApp()

const wxCharts = require('../../../utils/wxcharts')
var yuelineChart = null
var importlineChart = null

import util from '../../../utils/util'
import {
  GetAdminEventCount,
  GetEventCount,
  GetWringEventCount,
  GetAdminWringEventCount
} from '../../../config/api'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    adminInfo: '',
    ordinary: [],
    important: [],
    grider_storge: '',
    event_level: 1,
    year: '2020',

    num: 7,
    warning_data: '',
    day_index: 0,
    objectArray: [{
        num: 7,
        name: '7天内'
      },
      {
        num: 15,
        name: '15天内'
      },
      {
        num: 30,
        name: '30天内'
      },
      {
        num: 365,
        name: '一年内'
      }
    ],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let that = this;
    let adminInfo = wx.getStorageSync('adminInfo')
    let grider_storge = wx.getStorageSync('grider')
    this.setData({
      adminInfo,
      grider_storge
    })

    if (adminInfo) { // 判断管理员
      this.getAdminOrdinaryList()
      this.getAdminImportList()
      this.getAdminWarningCount()
    } else {
      this.getOrdinaryList()
      this.getImportList()
      this.getWarningCount() // 预警数据
    }
  },

  bindDateChange: function(e) { // 事件等级选择年份
    console.log('携带值为', e.detail.value)
    this.setData({
      year: e.detail.value
    })

    if (this.data.adminInfo) { // 判断管理员
      this.getAdminOrdinaryList()
      this.getAdminOrdinaryList()
    } else {
      this.getOrdinaryList()
      this.getImportList()
    }

  },

  touchHandler: function(e) { // 点击点事件
    console.log('普通点击', yuelineChart.getCurrentDataIndex(e));
    yuelineChart.showToolTip(e, {
      // background: '#7cb5ec',
      format: function(item, category) {
        console.log('item:', item)
        return category + '月' + ' ' + item.name + ':' + item.data
      }
    });
  },

  touchImportHandler: function(e) { // 重点事件点击点事件
    console.log('重点点击', importlineChart.getCurrentDataIndex(e));
    importlineChart.showToolTip(e, {
      // background: '#7cb5ec',
      format: function(item, category) {
        console.log('item:', item)
        return category + '月' + ' ' + item.name + ':' + item.data
      }
    });
  },

  getAdminOrdinaryList() { // 管理员普通事件列表
    let that = this
    let params = {
      vollege_id: app.globalData.vollege_id,
      event_level: 1,
      year: that.data.year
    }
    util.apiRequest(GetAdminEventCount, params, null).then(r => {
      console.log('管理员事件数据：', r)
      let arr = r.data.data
      let eventNumsArr = arr.map(item => {
        return item.num
      })
      that.setData({
        ordinary: eventNumsArr
      })
      that.getMothEvents()
    })
  },

  getOrdinaryList() { // 获取普通事件
    let that = this;
    let params = {
      vollege_id: app.globalData.vollege_id,
      grider_id: that.data.grider_storge.id,
      event_level: 1,
      year: that.data.year
    }
    util.apiRequest(GetEventCount, params, null).then(r => {
      console.log('事件数据：', r)
      let arr = r.data.data
      let eventNumsArr = arr.map(item => {
        return item.num
      })
      that.setData({
        ordinary: eventNumsArr
      })
      that.getMothEvents()
    })
  },

  getMothEvents: function() { // 普通事件折线图
    let that = this;
    var windowWidth = 320;
    try {
      var res = wx.getSystemInfoSync();
      windowWidth = res.windowWidth;
      console.log('宽度：', windowWidth)
    } catch (e) {
      console.error('getSystemInfoSync failed!');
    }

    yuelineChart = new wxCharts({ // 普通事件折线图配置
      canvasId: 'yueEle',
      type: 'line',
      categories: ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12'], //categories X轴
      animation: true,
      title: {
        fontSize: 28
      },
      // background: '#f5f5f5',
      series: [{
        color: '#29be74',
        name: '普通事件',
        data: that.data.ordinary,
        format: function(val, name) {
          return val;
        }
      }],
      xAxis: {
        disableGrid: true
      },
      yAxis: {
        title: '当月事件数量(件)',
        format: function(val) {
          return parseInt(val);
        },
        //  max: 20,
        //  min: 0
      },
      width: windowWidth,
      height: 240,
      dataLabel: false,
      dataPointShape: true,
      extra: {
        lineStyle: 'curve'
      }
    });
  },

  getAdminImportList() { // 管理重点事件列表
    let that = this
    let params = {
      vollege_id: app.globalData.vollege_id,
      event_level: 2,
      year: that.data.year
    }
    util.apiRequest(GetAdminEventCount, params, null).then(r => {
      console.log('管理员重点事件数据：', r)
      let arr = r.data.data
      let eventNumsArr = arr.map(item => {
        return item.num
      })
      that.setData({
        important: eventNumsArr
      })
      that.getImportEvents()
    })
  },

  getImportList() { // 重点事件
    let that = this;
    let params = {
      vollege_id: app.globalData.vollege_id,
      grider_id: that.data.grider_storge.id,
      event_level: 2,
      year: that.data.year
    }
    util.apiRequest(GetEventCount, params, null).then(r => {
      console.log('事件数据：', r)
      let arr = r.data.data
      let eventNumsArr = arr.map(item => {
        return item.num
      })

      that.setData({
        important: eventNumsArr
      })
      that.getImportEvents()
    })
  },

  getImportEvents: function() {
    let that = this;
    var windowWidth = 320;
    try {
      var res = wx.getSystemInfoSync();
      windowWidth = res.windowWidth;
      console.log('宽度：', windowWidth)
    } catch (e) {
      console.error('getSystemInfoSync failed!');
    }

    importlineChart = new wxCharts({ // 重点事件折线图
      canvasId: 'importEle',
      type: 'line',
      categories: ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12'], //categories X轴
      animation: true,
      // background: '#f5f5f5',
      series: [{
        name: '重点事件',
        data: that.data.important,
        format: function(val, name) {
          return val;
        }
      }],
      xAxis: {
        disableGrid: true
      },
      yAxis: {
        title: '当月事件数量(件)',
        format: function(val) {
          return parseInt(val);
        },
        //  max: 20,
        //  min: 0
      },
      width: windowWidth,
      height: 240,
      dataLabel: false,
      dataPointShape: true,
      extra: {
        lineStyle: 'curve'
      }
    });
  },

  // 预警

  bindPickerChange: function(e) { // 选择事件状态
    console.log('携带值为', e.detail.value)
    let num = this.data.objectArray[e.detail.value].num
    this.setData({
      day_index: e.detail.value,
      num
    })

    if (this.data.adminInfo) { // 判断管理员
      this.getAdminWarningCount()
    } else {
      this.getWarningCount()
    }
  },


  getAdminWarningCount() { // 管理员数据预警统计
    let that = this;
    let params = {
      vollege_id: app.globalData.vollege_id,
      num: that.data.num
    }
    util.apiRequest(GetAdminWringEventCount, params, null).then(r => {
      console.log('管理员预警r', r)
      that.setData({
        warning_data: r.data.data
      })
    })
  },

  getWarningCount() { // 网格员、长角色
    let that = this;
    let params = {
      vollege_id: app.globalData.vollege_id,
      grider_id: this.data.grider_storge.id,
      num: that.data.num
    }
    util.apiRequest(GetWringEventCount, params, null).then(r => {
      console.log('预警r', r)
      that.setData({
        warning_data: r.data.data
      })
    })
  },

  checkEvent(e) {
    let num = e.currentTarget.dataset.num
    let status = e.currentTarget.dataset.status
    wx.navigateTo({
      url: '/pages/grid/events_warning/events_warning_list/events_warning_list?num=' + num + '&status=' + status,
    })
  },

  // END 预警

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})